package com.godme.structure;

public class BinarySearch {



    static int binarySearch(int[] src, int target){
        int low = 0;
        int high = src.length - 1;
        int mid;

        while(low < high && src[low] < target && target < src[high]){
            mid = (high + low) / 2;
            if(target < src[mid]){
                high = mid;
            } else {
                low = mid;
            }
        }

        return src[low] == target ? low : high;
    }
    static int binarySearch2(int[] src, int target, int start, int end){
        if(start == end){
            return -1;
        }
        int mid = (start + end) / 2;
        if(src[mid] == target){
            return mid;
        }
        if(src[mid] < target){
            return binarySearch2(src, target, mid, end);
        }
        return binarySearch2(src, target, start, mid);
    }


    public static void main(String[] args) {
        int[] arr = new int[]{0,1,2,3,4,5,6,7,8,9};
//        System.err.println(binarySearch(arr, 5));
        System.err.println(binarySearch2(arr, 9, 0, arr.length));

    }

}
